import { Service } from "@/middleware/response";
import { RoleModel } from "@/models/admin/Role";
import { Role } from "@/types/admin/role";
import { PageReq } from "@/types/common/common";
import {
    catchError,
    createRecord,
    deleteRecord,
    getPageList,
    GetPageListOptions,
    getRecord,
    updateRecord,
    GetChildrenPageListOptions,
    getChildrenPageList,
    GetRecordOptions,
    CreateRecordOptions,
    DeleteRecordOptions,
} from "@/utils/db";
import { AdminUserModel } from "@/models/admin/User";
import { PermissionModel } from "@/models/admin/Permission";
import { setUserRolePermission } from "@/middleware/admin/auth";

export const getRoleListService: Service = async (req, res) => {
    const { page, pageSize, ...match } = req.query;
    const { name, key } = match;
    const options: GetPageListOptions<Role> = {
        model: RoleModel,
        params: req.query as PageReq,
        $project: { permissions: 0 },
    };
    if (name || key) options.$match = {};

    if (options.$match) {
        if (name && typeof name === "string") options.$match.name = { $regex: RegExp(name), $options: "i" };
        if (key && typeof key === "string") options.$match.key = { $regex: RegExp(key), $options: "i" };
    }
    catchError({
        async operation() {
            const { total, list } = await getPageList(options);
            res.sendResponse!(200, { total, list });
        },
        res,
        type: "查询",
    });
};
export const getRoleDetailService: Service = async (req, res) => {
    const { _id } = req.params;
    const childFields: GetRecordOptions<Role>["childFields"] = [{ localField: "permissions", from: "permissions" }];
    catchError({
        async operation() {
            return await getRecord({ model: RoleModel, _id, childFields });
        },
        res,
        type: "查询",
    });
};
export const createRoleService: Service = async (req, res) => {
    const { permissions, ...data } = req.body;
    catchError({
        async operation() {
            const childFields: CreateRecordOptions<Role>["childFields"] = [{ key: "permissions", value: permissions, model: PermissionModel }];
            await createRecord({ model: RoleModel, childFields, data });
        },
        res,
        type: "创建",
    });
};
export const updateRoleService: Service = async (req, res) => {
    const { _id, ...data } = req.body;
    catchError({
        async operation() {
            await updateRecord({ model: RoleModel, _id, data });
            await setUserRolePermission(req, res, true);
        },
        res,
        type: "更新",
    });
};
export const deleteRoleService: Service = async (req, res) => {
    const { _id } = req.body;
    const childFields: DeleteRecordOptions<Role>["childFields"] = [{ key: "permissions", model: PermissionModel }];
    catchError({
        async operation() {
            await deleteRecord({ model: RoleModel, _id, childFields });
        },
        res,
        type: "删除",
    });
};
export const getItemsService: Service = async (req, res) => {
    const { _id, ...match } = req.query;
    const { name, key } = match;
    const options: GetChildrenPageListOptions<Role> = {
        model: RoleModel,
        params: req.query as PageReq,
        parentKey: "_id",
        parentValue: _id,
        parentModel: AdminUserModel,
        localField: "roles",
        foreignField: "_id",
    };
    if (name || key) options.$match = {};

    if (options.$match) {
        if (typeof name === "string") options.$match.name = { $regex: RegExp(name), $options: "i" };
        if (typeof key === "string") options.$match.key = { $regex: RegExp(key), $options: "i" };
    }
    catchError({
        async operation() {
            const result = await getChildrenPageList(options);
            res.sendResponse!(200, result);
        },
        res,
        type: "查询",
    });
};
